import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
// 改造上面语句成路由懒加载语句
// webpackChunkName: "login-home-welcome"  是把三个组件都打包到同一个js中
const Login = () => import(/* webpackChunkName: "login-home-welcome" */ '../components/Login.vue')
// 下面的改造同理

// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "login-home-welcome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login-home-welcome" */ '../components/Welcome.vue')

// import Users from '../components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "users-rights-roles" */ '../components/user/Users.vue')
// import Rights from '../components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "users-rights-roles" */ '../components/power/Rights.vue')
// import Roles from '../components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "users-rights-roles" */ '../components/power/Roles.vue')

// import Cate from '../components/goods/Cate.vue'
const Cate = () => import(/* webpackChunkName: "cate-params" */ '../components/goods/Cate.vue')
// import Params from '../components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "cate-params" */ '../components/goods/Params.vue')

// import List from '../components/goods/List.vue'
const List = () => import(/* webpackChunkName: "list-addgoods" */ '../components/goods/List.vue')
// import Addgoods from '../components/goods/Addgoods.vue'
const Addgoods = () => import(/* webpackChunkName: "list-addgoods" */ '../components/goods/Addgoods.vue')

// import Order from '../components/order/Order.vue'
const Order = () => import(/* webpackChunkName: "order-report" */ '../components/order/Order.vue')
// import Report from '../components/report/Report.vue'
const Report = () => import(/* webpackChunkName: "order-report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

// 路由规则的定义
const routes = [
  // 重定向的路由规则（访问根路径时）
  { path: '/', redirect: '/login' },
  // 登录组件的路由
  { path: '/login', component: Login },
  // 主页组件路由
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Addgoods },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

// 前端路由管理器
const router = new VueRouter({
  // 路由规则
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
/* beforeEach接收一个回调函数，函数传入三个参数：
   to 将要访问的路径
   from 代表从哪个路径跳转而来
   next 是一个函数，表示放行
      next() 放行     next('/login') 强制跳转到指定路径
*/
  // 如果访问的是登录页，直接放行
  if (to.path === '/login') return next()
  // 如果访问的不是登录页，要获取token来验证是否登录过
  // 获取token(从浏览器存储中获取)
  const tokenStr = sessionStorage.getItem('token')
  // 如果没有token，则强制跳转到登录页
  if (!tokenStr) return next('/login')
  // 如果有也是直接放行
  next()
})

export default router
